// Figure B3 (APPENDIX)

eststo clear

// Declare outcome variable
global y real_ada

//Decleare kernel
global kernel uniform

//Declare running variable
global run dgap
global run2 dgap dwgap


/// LOOP
foreach sample in 1 2 3 4 5 6 7 8 9 10  {
use "data/table2/table2.dta", clear

// Shorter names
g d=dem
g m=maj
g dm=demmaj

keep if sample`sample'==1

//time dummies
quietly: tab yr, g(dcon)



 rdbwselect $y $run, kernel($kernel) 
	forval i = 2(1)17 {
	scalar h`i'=`i'*e(h_mserd)/10
	g bw`i'=0
	replace bw`i'=1 if $run > -`=h`i'' & $run < `=h`i''
	} 
	
	
	forval i = 2(1)17 {
		reg $y d $run2 if bw`i'==1, rob 
			scalar bA`i'= _b[d]
			scalar seA`i'= _se[d]

		reg $y d m dcon* $run2 if bw`i'==1, rob 
			scalar bD`i'= _b[d]
			scalar seD`i'= _se[d]
		
		matrix a`i' =[h`i',bA`i',seA`i',bD`i',seD`i']

		} 
	
	matrix a=[a2 \ a3 \ a4 \ a5 \	a6 \	a7 \	a8 \	a9 \	a10 \	a11 \	a12 \	a13 \	a14 \	a15 \	a16 \	a17 ]
		
	svmat a
	rename a1 h
	label variable h "Bandwidth"
	
	rename a2 bA
	rename a3 seA
	g upA = bA+1.96*seA
	g lowA =  bA-1.96*seA
	
	
	rename a4 bD
	rename a5 seD
	g upD = bD+1.96*seD
	g lowD =  bD-1.96*seD
	
	foreach x in bD upD lowD {
	replace `x'=. if seD==0
	}

	
	line  bA upA lowA bD upD lowD h, xline(`=h10')  title(`: variable label sample`sample'', size(medium) color(black)) graphregion(color(white))   ///
			lcolor (black black black red red red) ///
			lstyle(solid shortdash shortdash solid shortdash shortdash) ///
			 yscale(r(30 80)) ylabel(30(10)80) ///
			legend(order(1 4) label(1 "Short")  label(4 "Long+period-FE") rows(1)  region(lcolor(white) lwidth(0)))
	
	
	graph save "results/ada_sample`sample'.gph" , replace

	scalar drop _all
	}


// graph combine
grc1leg results/ada_sample6.gph results/ada_sample4.gph results/ada_sample5.gph results/ada_sample8.gph results/ada_sample7.gph  ///
		results/ada_sample1.gph results/ada_sample2.gph results/ada_sample3.gph results/ada_sample10.gph results/ada_sample9.gph ///
 , legendfrom(results/ada_sample1.gph) graphregion(color(white)) rows(2) 
graph display, ysize(10) xsize(18)

// export final graph
graph export "results/ada_anderson.pdf", replace



// erase single graphs
foreach f in grc1leg results/ada_sample6.gph results/ada_sample4.gph results/ada_sample5.gph results/ada_sample8.gph results/ada_sample7.gph  ///
		results/ada_sample1.gph results/ada_sample2.gph results/ada_sample3.gph results/ada_sample10.gph results/ada_sample9.gph {
	cap erase "`f'"
}
